﻿namespace ctolua.Models.UI
{
    /// <summary>
    /// UI模块
    /// </summary>
    public class MPrefab
    {
        /// <summary>
        /// 模块对象 打印会报错
        /// </summary>
        public string? id { get; set; }
        /// <summary>
        /// 模块预设id
        /// </summary>
        public string? modelId { get; set; }
        /// <summary>
        /// 玩家
        /// </summary>
        public Role role = MRole.getOwnerRole.role;
        /// <summary>
        /// 父ui名字
        /// </summary>
        public string? parent { get; set; }

        public MPrefab()
        {

        }
        /// <summary>
        /// 创建界面模块
        /// </summary>
        /// <param name="role ">  玩家   </param>
        /// <param name="prefab_id ">  预设uid   </param>
        /// <param name="comp_name ">  父控件   </param>
        /// <returns>uid</returns>
        public MPrefab(Role role, string prefab_id, string comp_name)
        {
            this.role = role;
            this.modelId = prefab_id;
            this.parent = comp_name;

            this.id = game_api.create_ui_prefab_instance(role, prefab_id, Mui.get_absolute_path(this.role, comp_name));
        }
        public MPrefab(MPrefab p)
        {
            this.id = game_api.create_ui_prefab_instance(p.role, p.modelId, Mui.get_absolute_path(this.role, p.parent));
        }
        /// <summary>
        /// 获得模块控件子UI
        /// </summary>
        /// <typeparam name="T">Model.control</typeparam>
        /// <param name="path">路径</param>
        /// <returns></returns>
        public T child<T>(string path) where T : Mui, new()
        {
            T t = new T();
            t.id = game_api.get_ui_prefab_child_by_path(this.id, path);
            return t;
        }
        /// <summary>
        /// 删除模块
        /// </summary>
        public void del()
        {
            game_api.del_ui_prefab(this.id);
        }
    }
}
